#!/usr/bin/perl -w # for syntax hilighting

use gri_database;
use Data::Dumper;

sub mm_form_docrepos_admin_modify_section_populate {
my ($section,$sec_parent,$sec_title,$sec_description,$sec_readers,$sec_publishers,$sec_attributes)=@_;

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $DB=new gri_database("$db_dir/gri.db");
	$section="" if $section eq "?section - not defined?";
	$sec_parent="" if $sec_parent eq "?sec_parent - not defined?";
	$sec_title="" if $sec_title eq "?sec_title - not defined?";
	$sec_description="" if $sec_description eq "?sec_description - not defined?";
	$sec_readers="" if $sec_readers eq "?sec_readers - not defined?";
	$sec_publishers="" if $sec_publishers eq "?sec_publishers - not defined?";
	$sec_attributes="" if $sec_attributes eq "?sec_attributes - not defined?";

	
	# If we have no details we need to get details from the database using
	# the section name.

	$section=hex_to_string($section);
	if($sec_title eq "") {
		my ($rc,$info)=$DB->get_section(section => $section);
		if($rc!=1) {
			return "Error: Section '$section' is not defined.";
		}
		# name,parent,description,title,reader_groups,publisher_groups
		$sec_parent=$info->{parent};
		$sec_title=$info->{title};
		$sec_description=$info->{description};
		$sec_readers=$info->{reader_groups};
		$sec_publishers=$info->{publisher_groups};
		$sec_attributes=$info->{attributes};
	} else {
		$sec_parent=hex_to_string($sec_parent);
		$sec_title=hex_to_string($sec_title);
		$sec_description=hex_to_string($sec_description);
		$sec_readers=hex_to_string($sec_readers);
		$sec_publishers=hex_to_string($sec_publishers);
		$sec_attributes=hex_to_string($sec_attributes);
	}
my $s="
    <tr class=gri_headerrow>
      <td class=gri_cell colspan=3>Existing Document Repository Section Details</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Name:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>
        <INPUT TYPE=\"text\" VALUE=\"$section\" NAME=\"name\" SIZE=\"60\" DISABLED MAXLENGTH=\"40\">
        <input type=hidden name=section value=\"$section\"/>
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Title:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>
        <INPUT TYPE=\"text\" VALUE=\"$sec_title\" NAME=\"title\" SIZE=\"60\" MAXLENGTH=\"80\">
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Description:</font></td>
      <td>&nbsp;&nbsp;&nbsp;</td>
      <td align=left>
        <TEXTAREA NAME=\"description\" COLS=60 ROWS=5>$sec_description</TEXTAREA>
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Attributes:</font></td>
      <td>&nbsp;&nbsp;&nbsp;</td>
      <td align=left>
        <INPUT TYPE=\"text\" SIZE=\"60\" NAME=\"attributes\" VALUE=\"$sec_attributes\">
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Parent Section:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>
	<SELECT NAME=\"parent\" SIZE=\"3\">\n";
	# Now we need a list of all existing sections and a special "_TOP_LEVEL_ entry too
	my ($rc,$lref)=$DB->list_sections();
	if($rc==-1) {
		$s.="<OPTION value=\"\" selected=\"selected\">Error: Unable to check groups</OPTION>\n";
	} else {
		for my $cc (@$lref) {
			next if $cc eq $section;
			if($sec_parent eq $cc) {
				$s.="<OPTION value=\"$cc\" SELECTED>$cc</OPTION>\n";
			} else {
				$s.="<OPTION value=\"$cc\">$cc</OPTION>\n";
			}
		}
		$s.="<OPTION value=\"\">&lt;None&gt;</OPTION>\n";
	}
	$s.="</SELECT>
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Reader Groups:</font></td>
      <td>&nbsp;&nbsp;&nbsp;</td>
      <td align=left>\n";

    # Add the tickboxes for the reader groups...
	my ($rc2,$lref2)=$DB->list_groups();
	my @existing_groups=split(/,/,$sec_readers);
	for my $cg (@$lref2) {
		my ($_l,$_h)=$DB->get_group(group=>$cg);
		if(grep {$cg eq $_} @existing_groups) {
			$s.="<INPUT TYPE=CHECKBOX NAME=\"reader_$cg\" CHECKED>$cg - $_h->{description}<BR>\n";
		} else {
			$s.="<INPUT TYPE=CHECKBOX NAME=\"reader_$cg\">$cg - $_h->{description}<BR>\n";
		}
	}
	$s.="\n</td></tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Publisher Groups:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>\n";

    # Add the tickboxes for the publisher groups...
	@existing_groups=split(/,/,$sec_publishers);
	for my $cg (@$lref2) {
		my ($_l,$_h)=$DB->get_group(group=>$cg);
		if(grep {$cg eq $_} @existing_groups) {
			$s.="<INPUT TYPE=CHECKBOX NAME=\"publisher_$cg\" CHECKED>$cg - $_h->{description}<BR>\n";
		} else {
			$s.="<INPUT TYPE=CHECKBOX NAME=\"publisher_$cg\">$cg - $_h->{description}<BR>\n";
		}
	}
	$s.="\n</td></tr>\n";
	return $s;
}

1;

# vim: ts=4:sw=4
